<template>
  <div>
    <template v-if="!loading && parentId === '#'">
      <el-button v-if="dataSource.length === 0" type="primary" @click="visible = true" icon="el-icon-circle-plus-outline">新增</el-button>
    </template>
    <el-table ref="tableRef" v-loading="loading" :data="dataSource" :show-header="parentId === '#'" row-key="branchId"
              :default-expand-all="parentId === '#'" empty-text="暂无下级组织"
    >
      <el-table-column type="expand" align="center" label="下级">
        <group-table slot-scope="scope" :parent-id="scope.row.branchId" style="padding-left: 12px" />
      </el-table-column>
      <el-table-column prop="groupName" label="名称" align="center"></el-table-column>
      <el-table-column prop="groupCode" label="编码" align="center"></el-table-column>
      <el-table-column prop="groupAllName" label="全称" align="center"></el-table-column>
      <el-table-column prop="groupIntroduce" label="介绍" align="center"></el-table-column>
      <el-table-column prop="dataState" label="状态" align="center" width="80px">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.dataState >=0 ">正常</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="130px">
        <div slot-scope="scope">
          <el-button type="success" icon="el-icon-edit" circle @click="() => { entityInfo=$Util.copy(scope.row);visible=true}" title="编辑"></el-button>
          <el-button type="warning" icon="el-icon-circle-plus-outline" circle @click="() => { entityInfo.parentId=scope.row.branchId;visible=true}" title="添加"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="removeData(scope.row, scope.$index)" title="删除"></el-button>
        </div>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="visible" title="菜单信息">
      <el-form label-position="right" label-width="80px">
        <el-form-item label="名称" required>
          <el-input v-model="entityInfo.groupName" placeholder="组织名称"/>
        </el-form-item>
        <el-form-item label="编码" required>
          <el-input v-model="entityInfo.groupCode" placeholder="组织编码,填写后不可修改" :disabled="!$Util.isEmpty(entityInfo.branchId)"/>
        </el-form-item>
        <el-form-item label="介绍">
          <el-input type="textarea" :rows="4"  v-model="entityInfo.groupIntroduce" placeholder="组织介绍"/>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="entityInfo.dataState">
            <el-radio :label="-1">禁用</el-radio>
            <el-radio :label="0">正常</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-circle-check" type="success" @click="saveData(entityInfo, false, tableExpend)">{{ entityInfo.branchId ? '更新' : '保存' }}</el-button>
          <el-button icon="el-icon-circle-close" type="info" @click="visible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import IdeaTable from "@/mixins/IdeaTable";

export default {
  name: "GroupTable",
  mixins: [IdeaTable],
  props: {
    parentId: {
      type: String,
      default: '#'
    }
  },
  created() {
    this.queryUrl = '/group/queryPage'
    this.saveUrl = '/group/save'
    this.removeUrl = '/group/delete/'
    this.fixedFilters.push({ field: 'parentId', op: '=', value: this.parentId })
    this.rowKey = 'branchId'
    this.pageInfo.pageSize = 20
    this.loadSource()
    this.initEntity()
  },
  methods: {
    initEntity() {
      this.entityInfo = { parentId: this.parentId, dataState: 0 }
    },
    tableExpend(res) {
      if (res.code === 200 && this.parentId === '#') {
        const $table = this.$refs.tableRef
        $table.toggleRowExpansion(this.dataSource[0],false)
      }
    }
  }
}
</script>

<style scoped>

</style>
